[[...path]].page.tsx 9.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248
  1. import React from 'react';
  2. import { IUserHasId } from '@growi/core';
  3. import {
  4. GetServerSideProps, GetServerSidePropsContext,
  5. } from 'next';
  6. import { serverSideTranslations } from 'next-i18next/serverSideTranslations';
  7. import dynamic from 'next/dynamic';
  8. import Head from 'next/head';
  9. import { useCurrentGrowiLayoutFluidClassName } from '~/client/services/layout';
  10. import { MainPane } from '~/components/Layout/MainPane';
  11. import { ShareLinkLayout } from '~/components/Layout/ShareLinkLayout';
  12. import GrowiContextualSubNavigation from '~/components/Navbar/GrowiContextualSubNavigation';
  13. import { Page } from '~/components/Page';
  14. import type { PageSideContentsProps } from '~/components/PageSideContents';
  15. import { DrawioViewerScript } from '~/components/Script/DrawioViewerScript';
  16. import { SupportedAction, SupportedActionType } from '~/interfaces/activity';
  17. import { CrowiRequest } from '~/interfaces/crowi-request';
  18. import { RendererConfig } from '~/interfaces/services/renderer';
  19. import { IShareLinkHasId } from '~/interfaces/share-link';
  20. import {
  21. useCurrentUser, useCurrentPathname, useCurrentPageId, useRendererConfig, useIsSearchPage,
  22. useShareLinkId, useIsSearchServiceConfigured, useIsSearchServiceReachable, useIsSearchScopeChildrenAsDefault, useDrawioUri, useIsContainerFluid,
  23. } from '~/stores/context';
  24. import loggerFactory from '~/utils/logger';
  25. import { NextPageWithLayout } from '../_app.page';
  26. import {
  27. CommonProps, getServerSideCommonProps, generateCustomTitle, getNextI18NextConfig,
  28. } from '../utils/commons';
  29. const logger = loggerFactory('growi:next-page:share');
  30. const PageSideContents = dynamic<PageSideContentsProps>(() => import('~/components/PageSideContents').then(mod => mod.PageSideContents), { ssr: false });
  31. // const Comments = dynamic(() => import('~/components/Comments').then(mod => mod.Comments), { ssr: false });
  32. const ShareLinkAlert = dynamic(() => import('~/components/Page/ShareLinkAlert'), { ssr: false });
  33. const ForbiddenPage = dynamic(() => import('~/components/ForbiddenPage'), { ssr: false });
  34. type Props = CommonProps & {
  35. shareLink?: IShareLinkHasId,
  36. isExpired: boolean,
  37. disableLinkSharing: boolean,
  38. isSearchServiceConfigured: boolean,
  39. isSearchServiceReachable: boolean,
  40. isSearchScopeChildrenAsDefault: boolean,
  41. drawioUri: string | null,
  42. rendererConfig: RendererConfig,
  43. };
  44. const SharedPage: NextPageWithLayout<Props> = (props: Props) => {
  45. useIsSearchPage(false);
  46. useShareLinkId(props.shareLink?._id);
  47. useCurrentPageId(props.shareLink?.relatedPage._id);
  48. useCurrentUser(props.currentUser);
  49. useCurrentPathname(props.currentPathname);
  50. useRendererConfig(props.rendererConfig);
  51. useIsSearchServiceConfigured(props.isSearchServiceConfigured);
  52. useIsSearchServiceReachable(props.isSearchServiceReachable);
  53. useIsSearchScopeChildrenAsDefault(props.isSearchScopeChildrenAsDefault);
  54. useDrawioUri(props.drawioUri);
  55. useIsContainerFluid(props.isContainerFluid);
  56. const growiLayoutFluidClass = useCurrentGrowiLayoutFluidClassName();
  57. const isNotFound = props.shareLink == null || props.shareLink.relatedPage == null || props.shareLink.relatedPage.isEmpty;
  58. const isShowSharedPage = !props.disableLinkSharing && !isNotFound && !props.isExpired;
  59. const shareLink = props.shareLink;
  60. const title = generateCustomTitle(props, 'GROWI');
  61. const sideContents = shareLink != null
  62. ? <PageSideContents page={shareLink.relatedPage} />
  63. : <></>;
  64. // const footerContents = shareLink != null && isPopulated(shareLink.relatedPage.revision)
  65. // ? (
  66. // <>
  67. // <Comments pageId={shareLink._id} pagePath={shareLink.relatedPage.path} revision={shareLink.relatedPage.revision} />
  68. // </>
  69. // )
  70. // : <></>;
  71. return (
  72. <>
  73. <Head>
  74. <title>{title}</title>
  75. </Head>
  76. <div className={`dynamic-layout-root ${growiLayoutFluidClass} h-100 d-flex flex-column justify-content-between`}>
  77. <header className="py-0 position-relative">
  78. {isShowSharedPage && <GrowiContextualSubNavigation isLinkSharingDisabled={props.disableLinkSharing} />}
  79. </header>
  80. <div id="grw-fav-sticky-trigger" className="sticky-top"></div>
  81. <MainPane
  82. sideContents={sideContents}
  83. // footerContents={footerContents}
  84. >
  85. { props.disableLinkSharing && (
  86. <div className="mt-4">
  87. <ForbiddenPage isLinkSharingDisabled={props.disableLinkSharing} />
  88. </div>
  89. )}
  90. { (isNotFound && !props.disableLinkSharing) && (
  91. <div className="container-lg">
  92. <h2 className="text-muted mt-4">
  93. <i className="icon-ban" aria-hidden="true" />
  94. <span> Page is not found</span>
  95. </h2>
  96. </div>
  97. )}
  98. { (props.isExpired && !props.disableLinkSharing && shareLink != null) && (
  99. <div className="container-lg">
  100. <ShareLinkAlert expiredAt={shareLink.expiredAt} createdAt={shareLink.createdAt} />
  101. <h2 className="text-muted mt-4">
  102. <i className="icon-ban" aria-hidden="true" />
  103. <span> Page is expired</span>
  104. </h2>
  105. </div>
  106. )}
  107. {(isShowSharedPage && shareLink != null) && (
  108. <>
  109. <ShareLinkAlert expiredAt={shareLink.expiredAt} createdAt={shareLink.createdAt} />
  110. <Page />
  111. </>
  112. )}
  113. </MainPane>
  114. </div>
  115. </>
  116. );
  117. };
  118. SharedPage.getLayout = function getLayout(page) {
  119. return (
  120. <>
  121. <DrawioViewerScript />
  122. <ShareLinkLayout>{page}</ShareLinkLayout>
  123. </>
  124. );
  125. };
  126. function injectServerConfigurations(context: GetServerSidePropsContext, props: Props): void {
  127. const req: CrowiRequest = context.req as CrowiRequest;
  128. const { crowi } = req;
  129. const { configManager, searchService, xssService } = crowi;
  130. props.disableLinkSharing = configManager.getConfig('crowi', 'security:disableLinkSharing');
  131. props.isSearchServiceConfigured = searchService.isConfigured;
  132. props.isSearchServiceReachable = searchService.isReachable;
  133. props.isSearchScopeChildrenAsDefault = configManager.getConfig('crowi', 'customize:isSearchScopeChildrenAsDefault');
  134. props.drawioUri = configManager.getConfig('crowi', 'app:drawioUri');
  135. props.rendererConfig = {
  136. isEnabledLinebreaks: configManager.getConfig('markdown', 'markdown:isEnabledLinebreaks'),
  137. isEnabledLinebreaksInComments: configManager.getConfig('markdown', 'markdown:isEnabledLinebreaksInComments'),
  138. adminPreferredIndentSize: configManager.getConfig('markdown', 'markdown:adminPreferredIndentSize'),
  139. isIndentSizeForced: configManager.getConfig('markdown', 'markdown:isIndentSizeForced'),
  140. plantumlUri: process.env.PLANTUML_URI ?? null,
  141. blockdiagUri: process.env.BLOCKDIAG_URI ?? null,
  142. // XSS Options
  143. isEnabledXssPrevention: configManager.getConfig('markdown', 'markdown:rehypeSanitize:isEnabledPrevention'),
  144. attrWhiteList: xssService.getAttrWhiteList(),
  145. tagWhiteList: xssService.getTagWhiteList(),
  146. highlightJsStyleBorder: configManager.getConfig('crowi', 'customize:highlightJsStyleBorder'),
  147. };
  148. }
  149. async function injectNextI18NextConfigurations(context: GetServerSidePropsContext, props: Props, namespacesRequired?: string[] | undefined): Promise<void> {
  150. const nextI18NextConfig = await getNextI18NextConfig(serverSideTranslations, context, namespacesRequired);
  151. props._nextI18Next = nextI18NextConfig._nextI18Next;
  152. }
  153. function getAction(props: Props): SupportedActionType {
  154. let action: SupportedActionType;
  155. if (props.isExpired) {
  156. action = SupportedAction.ACTION_SHARE_LINK_EXPIRED_PAGE_VIEW;
  157. }
  158. else if (props.shareLink == null) {
  159. action = SupportedAction.ACTION_SHARE_LINK_NOT_FOUND;
  160. }
  161. else {
  162. action = SupportedAction.ACTION_SHARE_LINK_PAGE_VIEW;
  163. }
  164. return action;
  165. }
  166. async function addActivity(context: GetServerSidePropsContext, action: SupportedActionType): Promise<void> {
  167. const req: CrowiRequest = context.req as CrowiRequest;
  168. const parameters = {
  169. ip: req.ip,
  170. endpoint: req.originalUrl,
  171. action,
  172. user: req.user?._id,
  173. snapshot: {
  174. username: req.user?.username,
  175. },
  176. };
  177. await req.crowi.activityService.createActivity(parameters);
  178. }
  179. export const getServerSideProps: GetServerSideProps = async(context: GetServerSidePropsContext) => {
  180. const req = context.req as CrowiRequest<IUserHasId & any>;
  181. const { crowi, params } = req;
  182. const result = await getServerSideCommonProps(context);
  183. if (!('props' in result)) {
  184. throw new Error('invalid getSSP result');
  185. }
  186. const props: Props = result.props as Props;
  187. try {
  188. const ShareLinkModel = crowi.model('ShareLink');
  189. const shareLink = await ShareLinkModel.findOne({ _id: params.linkId }).populate('relatedPage');
  190. if (shareLink != null) {
  191. await shareLink.relatedPage.populateDataToShowRevision();
  192. props.isExpired = shareLink.isExpired();
  193. props.shareLink = shareLink.toObject();
  194. }
  195. }
  196. catch (err) {
  197. logger.error(err);
  198. }
  199. injectServerConfigurations(context, props);
  200. await injectNextI18NextConfigurations(context, props);
  201. await addActivity(context, getAction(props));
  202. return {
  203. props,
  204. };
  205. };
  206. export default SharedPage;